package net.atomarrow.render;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import net.atomarrow.ioc.ActionContext;

public class ImgRender extends Render {
    public static final String IMG_URL = "IMG_URL";
    public static final String IMG_INPUTSTREAM = "IMG_INPUTSTREAM";
    public static final String IMG_WIDTH = "IMG_WIDTH";
    public static final String IMG_HEIGHT = "IMG_HEIGHT";

    @Override
    public void render() {
        HttpServletResponse response = ActionContext.getContext().getResponse();
        response.reset();
        response.setContentType("image/jpg"); //设置返回的文件类型    
        ServletOutputStream out = null;
        InputStream inStream =null;
        Integer width =getAttr(IMG_WIDTH);
        Integer height =getAttr(IMG_HEIGHT);
        inStream = getAttr(IMG_INPUTSTREAM);
        if(inStream==null){
            String urlStr = getAttr(IMG_URL);
            if(urlStr==null){
                return;
            }
            if (urlStr.contains("http://")) {
                URL url;
                try {
                    url = new URL(urlStr);
                    HttpURLConnection conn = (HttpURLConnection) url
                            .openConnection();
                    conn.setRequestMethod("GET");
                    conn.setConnectTimeout(5 * 1000);
                    inStream = conn.getInputStream();// 通过输入流获取图片数据
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (ProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else{
                
            }
        }
        if(width!=null){
            //TODO #mmm
        }
        if(height!=null){
          //TODO #mmm
        }
        try {
            out = response.getOutputStream();
            byte[] b = new byte[4 * 1024];
            int len;
            while ((len = inStream.read(b)) > 0){
                out.write(b, 0, len);
            }
            response.setStatus(HttpServletResponse.SC_OK);
            response.flushBuffer();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(inStream!=null){
                try {
                    inStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
